package edu.cicese.ans;

import com.jsurf.SurfUtil;

import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    static {
		try {
			System.load("c:\\mingw\\bin\\LIBGCC_S_DW2-1.DLL");
			System.load("c:\\mingw\\bin\\LIBSTDC++-6.DLL");
//			System.load("c:\\windows\\system32\\MSVCRT.DLL");

//			System.loadLibrary("kernel32");
//			System.loadLibrary("msvcrt");
			System.loadLibrary("opensurf");
//			System.loadLibrary("cv210d");
//			System.loadLibrary("cxcore210d");
//			System.loadLibrary("highgui210d");
		} catch (UnsatisfiedLinkError e) {
			e.printStackTrace();
		}
	}

    public static void main(String[] args) {
    	new Server();
    }
    
    public Server() {



//
//	    ArrayList<InterestPoint> points1 = OpenSurf.getInterestPoints("D:\\Tests\\Experimento007\\003.jpg", 0.0001f);
//	    ArrayList<InterestPoint> points2 = OpenSurf.getInterestPoints("D:\\Tests\\Experimento007\\012.jpg", 0.0001f);
//
//	    long begin, end;
//		begin = System.currentTimeMillis();
//	    Map<InterestPoint, InterestPoint> matches = SurfUtil.getMatches(points1, points2);
//	    end = System.currentTimeMillis() - begin;
//		System.out.println("matches jcpp:" + matches.size() + ", " + end / 1000d + ", " + points1.size() + " " +
//		points2.size());

//	    OpenSurf.getMatches("D:\\Tests\\Experimento007\\003.jpg", "D:\\Tests\\Experimento007\\012.jpg");

	    // check the needed directories
	    Utilities.checkDirs();


	    // delete the Tag-changelog (because we are about to load ALL the tags anyway)
	    SurfUtil.cleanChangelog("TAGchangelog.ans");
	    // load tag repository
	    Utilities.loadingTags = true;
	    SurfUtil.loadAllTags();
	    Utilities.loadingTags = false;
	    // check the Tag-changelog for updates
	    SurfUtil.updateTags();


	    // delete the POI-changelog (because we are about to load ALL the POIs anyway)
	    SurfUtil.cleanChangelog("POIchangelog.ans");
	    // load all the repository points (to locate the user)
	    Utilities.loadingPOIs = true;
	    SurfUtil.loadPOIs();
	    Utilities.loadingPOIs = false;
	    // check the POI-changelog for updates
	    SurfUtil.updatePOIs();

	    // TODO: Review cluster use
//	    SurfUtil.loadClusters();

	    // TODO: Remove, not necessary
	    // check the tag-changelog
	    Utilities.getTreeChanges();


	    // TODO: Remove, test only
//	    SurfUtil.loadAllTags();

	    TPOIRepository POIRepositoryThread = new TPOIRepository();
	    POIRepositoryThread.start();

	    TTagRepository TagRepositoryThread = new TTagRepository();
	    TagRepositoryThread.start();



//	    Client.test2();
	    // Listen
	    listenClient();
    }
    
    //! Listens for an incoming petition from a client.
	public void listenClient() {
		try {
			ServerSocket socket = new ServerSocket(Utilities.SERVER_PORT);
			Utilities.print("Listening..");
//			System.out.println("Listening..");
			while (true)
            {
                Socket socketService = socket.accept();
                if (socketService != null)
                {
                    Client client = new Client(socketService);
                    client.start();
                }
            }
		}
		catch(Exception e) { e.printStackTrace(); }
	}

	private void test() {
		SurfUtil.speedTest();
//		SurfUtil.scaleCCheckTest();
//		SurfUtil.kdTreeTest();
	}
}